package com.firstgroup.dms.service.impl;

import com.firstgroup.dms.pojo.SupplierInformation;
import com.firstgroup.dms.mapper.SupplierInformationMapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import javax.annotation.Resource;

/**
 * (SupplierInformation)表服务实现类
 *
 * @author lumos
 * @since 2025-01-19 10:50:45
 */
@Service("supplierInformationService")
public class SupplierInformationServiceImpl {
    @Resource
    private SupplierInformationMapper supplierInformationMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", this.supplierInformationMapper.selectById(id));
        map.put("msg", "查询单条记录成功");
        return map;
    }

    /**
     * 一次性全部查询
     *
     * @return 查询结果
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "全部查询成功");
        map.put("data", this.supplierInformationMapper.selectAll());
        return map;
    }

    /**
     * 分页查询
     *
     * @param supplierInformation 筛选条件
     * @param page        要查询第几页
     * @param limit       每页显示多少条
     * @return 查询结果
     */
    public Map<String, Object> selectByLimit(SupplierInformation supplierInformation, int page, int limit) {
        Map<String, Object> map = new HashMap<>();
        try {
            // 计算起始下标
            int startIndex = (page - 1) * limit;
            
            // 获取数据列表
            List<SupplierInformation> list = this.supplierInformationMapper.selectByLimit(supplierInformation, startIndex, limit);
            
            // 获取总记录数
            long count = this.supplierInformationMapper.selectCount(supplierInformation);
            
            // 返回符合Layui表格要求的数据格式
            map.put("code", 0);  // Layui要求成功码为0
            map.put("msg", "");
            map.put("count", count);
            map.put("data", list);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "查询失败：" + e.getMessage());
            map.put("count", 0);
            map.put("data", new ArrayList<>());
        }
        return map;
    }
    
    /**
     * 新增数据
     *
     * @param supplierInformation 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(SupplierInformation supplierInformation) {
        Map<String, Object> map = new HashMap<>();
        try {
            // 确保状态值正确
            if (supplierInformation.getStatus() == null) {
                supplierInformation.setStatus(0);  // 设置默认值
            }
            
            int result = this.supplierInformationMapper.insert(supplierInformation);
            if (result > 0) {
                map.put("code", 200);
                map.put("data", supplierInformation);
                map.put("msg", "新增成功");
            } else {
                map.put("code", 500);
                map.put("msg", "新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "新增失败：" + e.getMessage());
        }
        return map;
    }

    /**
     * 修改数据
     *
     * @param supplierInformation 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(SupplierInformation supplierInformation) {
        this.supplierInformationMapper.updateById(supplierInformation);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", supplierInformation);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            // 这里可以添加业务检查逻辑
            int result = this.supplierInformationMapper.deleteById(id);
            map.put("code", 200);
            map.put("msg", result > 0 ? "删除成功" : "删除失败");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "删除失败：" + e.getMessage());
        }
        return map;
    }
}
